package com.jsjosem.aftersales.ui.worktickets;

import static com.jsjosem.aftersales.constant.Command.EVENT_CREATE_WORK_ORDER;
import static com.jsjosem.aftersales.utils.CommonUtils.getDepartmentsMenu;
import static com.jsjosem.aftersales.utils.CommonUtils.getDepartmentsMultipleChoiceMenu;
import static com.jsjosem.aftersales.utils.CommonUtils.selectLaterTimeDialog;
import static com.jsjosem.aftersales.utils.CommonUtils.selectPhoto;
import static com.jsjosem.aftersales.utils.CommonUtils.setSpinnerAdapter;
import static com.jsjosem.aftersales.utils.CommonUtils.showBigImg;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.adapter.CustomerUserAdapter;
import com.jsjosem.aftersales.adapter.HelperListAdapter;
import com.jsjosem.aftersales.adapter.ImgListViewAdapter;
import com.jsjosem.aftersales.adapter.ProductListAdapter;
import com.jsjosem.aftersales.api.RetrofitClient;
import com.jsjosem.aftersales.api.WorkOrderApi;
import com.jsjosem.aftersales.component.CustomGridView;
import com.jsjosem.aftersales.data.request.WorkOrderRequest;
import com.jsjosem.aftersales.data.response.BaseResponse;
import com.jsjosem.aftersales.data.response.CustomerResponse;
import com.jsjosem.aftersales.data.response.CustomerUserResponse;
import com.jsjosem.aftersales.data.response.DepartmentsUserResponse;
import com.jsjosem.aftersales.data.response.ProductResponse;
import com.jsjosem.aftersales.request.AsyncRequest;
import com.jsjosem.aftersales.request.CustomerUserRequest;
import com.jsjosem.aftersales.request.FileRequest;
import com.jsjosem.aftersales.ui.notifications.ServiceEventInfoActivity;
import com.jsjosem.aftersales.utils.ImmUtils;
import com.jsjosem.aftersales.utils.PermissionUtils;
import com.jsjosem.aftersales.utils.ScreenSizeUtils;
import com.jsjosem.aftersales.utils.ViewUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.wang.avi.AVLoadingIndicatorView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Retrofit;

public class MaintainActivity extends AppCompatActivity {
    private TextView timeTv, customerName, staffName, createCustomerUser, createCustomer, createProduct;
    private EditText customerContactsName, customerPhoneNum, customerAddress;
    private ConstraintLayout customerAddLay, customerInfoLay, workOrderPersonLay, faultPhoto, helperLay;
    private TextView customerContactsNameLay;
    private ImageView productAddImg, imBack, directoryImg;
    private CustomGridView faultImgList;
    private ListView productList, helperName;
    private ProductListAdapter productListAdapter;
    private ImgListViewAdapter imgListViewAdapter;
    private LinearLayout linearLayout, faultInfoItem;
    private Button saveBtn, saveOnlyBtn;
    private EditText faultInfo;
    private Spinner serviceContentSpinner, serviceTypeSpinner;
    private CustomerResponse customerItem;
    private CustomerUserResponse customerUser;
    private ArrayList<ProductResponse> productData;
    private String serviceContent, serviceType;
    private List<String> faultImgUrls;
    private DepartmentsUserResponse masterUser;
    private List<DepartmentsUserResponse> helperList;
    private List<CustomerUserResponse> customerUserResponses;
    private Intent intent;
    private HelperListAdapter helperAdapter;
    private CustomerUserAdapter customerUserAdapter;
    private final CustomerUserRequest customerUserRequest = new CustomerUserRequest();
    private Dialog loadingDialog;
    private final String[] serviceContentArray = {"检测", "检查", "安装", "维修", "调试"};
    private final String[] serviceTypeArray = {"保内免费", "保内收费", "保外免费", "保外收费"};
    private final String[] typeNameArray = {"维保工单", "调试工单", "安装工单", "培训工单"};
    private String eventId;
    private boolean isEventCreateWork = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintain);
        initView();
        initData();

        ImmUtils.setCloseKeyboard(linearLayout, this);

        if (intent.getIntExtra("serviceType", 0) == 1) {
            faultInfoItem.setVisibility(View.GONE);
        }
        if (intent.getIntExtra("event_create_work", 0) == EVENT_CREATE_WORK_ORDER) {
            isEventCreateWork = true;
        }

        timeTv.setOnClickListener(view -> {
            selectLaterTimeDialog(this, timeTv);
        });

        customerAddLay.setOnClickListener(view -> {
            Intent intent = new Intent(MaintainActivity.this, CustomerListActivity.class);
            startActivityForResult(intent, 1);
        });

        customerName.setOnClickListener(view -> {
            Intent intent = new Intent(MaintainActivity.this, CustomerListActivity.class);
            startActivityForResult(intent, 1);
        });

        //新建客户联系人
        createCustomerUser.setOnClickListener(view -> {
            Intent intent = new Intent(MaintainActivity.this, CreateCustomerUserActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("customer", customerItem);
            intent.putExtras(bundle);
            startActivityForResult(intent, 3);
        });

        //新建客户
        createCustomer.setOnClickListener(view -> {
            Intent intent = new Intent(MaintainActivity.this, CreateCustomerActivity.class);
            startActivityForResult(intent, 4);
        });

        //新建产品
        createProduct.setOnClickListener(view -> {
            if (customerItem.getId() == null) {
                Toast.makeText(MaintainActivity.this, "请先选择客户", Toast.LENGTH_SHORT).show();
                return;
            } else {
                Intent intent = new Intent(MaintainActivity.this, CreateProductActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("customer", customerItem);
                intent.putExtras(bundle);
                startActivityForResult(intent, 5);
            }
        });

        imBack.setOnClickListener(backClick -> {
            finish();
        });

        //选择客户产品
        productAddImg.setOnClickListener(view -> {
            if (customerItem.getId() == null) {
                Toast.makeText(MaintainActivity.this, "请先选择客户", Toast.LENGTH_SHORT).show();
                return;
            }
            Intent intent = new Intent(MaintainActivity.this, ProductListActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("customerItem", customerItem);
            intent.putExtras(bundle);
            startActivityForResult(intent, 2);
        });

        //服务内容
        serviceContent = serviceContentArray[0];
        setSpinnerAdapter(this, serviceContentSpinner, serviceContentArray, intent.getIntExtra("serviceType", 0));
        serviceContentSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                serviceContent = serviceContentArray[i];
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        //服务类型
        serviceType = serviceTypeArray[0];
        setSpinnerAdapter(this, serviceTypeSpinner, serviceTypeArray, 0);
        serviceTypeSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                serviceType = serviceTypeArray[i];
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        //选择图片
        faultPhoto.setOnClickListener(view -> selectPhoto(this));

        //图片放大
        faultImgList.setOnItemClickListener((adapterView, view, i, l) -> showBigImg(this, faultImgUrls, i));

        //选择区域负责人
        workOrderPersonLay.setOnClickListener(view -> {
            getDepartmentsMenu(this, directoryImg, staffName, masterUser);

        });

        //选择协助人
        helperLay.setOnClickListener(view -> {
            getDepartmentsMultipleChoiceMenu(this, helperName, helperList);
            helperAdapter.notifyDataSetChanged();
        });

        //点击删除协助人
        helperName.setOnItemClickListener((adapterView, view, i, l) -> {
            helperList.remove(i);
            helperAdapter.notifyDataSetChanged();
            ViewUtils.setListViewHeightBasedOnChildren(helperName);
        });

        //选择联系人
        customerContactsNameLay.setOnClickListener(view -> {
            View dialogView = View.inflate(this, R.layout.dialog_customer_user, null);
            Dialog dialog = createDialog(dialogView);
            ListView listView = dialogView.findViewById(R.id.customer_user_list);
            listView.setAdapter(customerUserAdapter);
            loadingShow();
            getCustomerUser(customerItem.getId());
            listView.setOnItemClickListener((adapterView, view1, i, l) -> {
                customerContactsName.setText(customerUserResponses.get(i).getName());
                customerPhoneNum.setText(customerUserResponses.get(i).getPhone());
                customerUser = customerUserResponses.get(i);
                dialog.dismiss();
            });
            dialog.show();
        });

        productList.setOnItemClickListener((adapterView, view, i, l) -> {
            productData.remove(i);
            productListAdapter.notifyDataSetChanged();
            ViewUtils.setListViewHeightBasedOnChildren(productList);
        });

        faultImgList.setOnItemLongClickListener((adapterView, view, i, l) -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示");
            builder.setMessage("是否删除该图片");
            builder.setPositiveButton("确定", (dialogInterface, i1) -> {
                faultImgUrls.remove(i);
                imgListViewAdapter.notifyDataSetChanged();
            });
            builder.setNegativeButton("取消", (dialogInterface, i12) -> {
            });
            builder.show();
            return true;
        });

        //保存上传
        saveBtn.setOnClickListener(view -> {
            WorkOrderRequest wo = new WorkOrderRequest();
            if (customerItem.getId() == null) {
                Toast.makeText(this, "请选择客户", Toast.LENGTH_SHORT).show();
                return;
            } else {
                wo.setmCustomerId(customerItem.getId());
                wo.setmProvinceId(customerItem.getProvince_id());
                wo.setmCityId(customerItem.getCity_id());
                wo.setmCountyId(customerItem.getCounty_id().toString());
                wo.setmAddress(customerItem.getDetail_address());
            }
            if (customerUser != null || (!customerContactsName.getText().toString().equals("") && !customerPhoneNum.getText().toString().equals(""))) {
                customerUser.setName(customerContactsName.getText().toString());
                customerUser.setPhone(customerPhoneNum.getText().toString());
                wo.setmContactName(customerUser.getName());
                wo.setmContactPhone(customerUser.getPhone());
            } else {
                Toast.makeText(this, "请选择联系人", Toast.LENGTH_SHORT).show();
                return;
            }
            if (productData.size() == 0) {
                Toast.makeText(this, "请添加产品", Toast.LENGTH_SHORT).show();
                return;
            } else {
                List<String> productIds = new ArrayList<>();
                for (int i = 0; i < productData.size(); i++) {
                    productIds.add(productData.get(i).getmId().toString());
                }
                wo.setmProductId(productIds);
            }
            if (timeTv.getText().toString().equals("请选择时间")) {
                Toast.makeText(this, "请选择时间", Toast.LENGTH_SHORT).show();
                return;
            } else {
                wo.setmPlanAt(timeTv.getText().toString());
            }
            if (TextUtils.isEmpty(faultInfo.getText().toString())) {
                Toast.makeText(this, "请填写故障描述", Toast.LENGTH_SHORT).show();
                return;
            } else {
                wo.setmDescription(faultInfo.getText().toString());
            }
            if (masterUser.getId() == null) {
                Toast.makeText(this, "请选择区域负责人", Toast.LENGTH_SHORT).show();
                return;
            } else {
                wo.setmUserId(Math.toIntExact(masterUser.getId()));
            }
            if (helperList.size() != 0) {
                List<String> userIds = new ArrayList<>();
                for (DepartmentsUserResponse userResponse : helperList) {
                    userIds.add(userResponse.getId().toString());
                }
                wo.setmAssistUserIds(userIds);
            }
            wo.setmLevel("普通");
            wo.setmTypeName(typeNameArray[0]);
            wo.setmServiceContent(serviceContent);
            wo.setmServiceType(serviceType);
            wo.setmEventId(eventId);
            uploadFile(faultImgUrls, wo);

        });

        //仅保存
        saveOnlyBtn.setOnClickListener(v -> {
            WorkOrderRequest wo = new WorkOrderRequest();
            if (customerItem.getId() == null) {
                Toast.makeText(this, "请选择客户", Toast.LENGTH_SHORT).show();
                return;
            } else {
                wo.setmCustomerId(customerItem.getId());
                wo.setmProvinceId(customerItem.getProvince_id());
                wo.setmCityId(customerItem.getCity_id());
                wo.setmCountyId(customerItem.getCounty_id().toString());
                wo.setmAddress(customerItem.getDetail_address());
            }
            if (productData.size() == 0) {
                Toast.makeText(this, "请添加产品", Toast.LENGTH_SHORT).show();
                return;
            } else {
                List<String> productIds = new ArrayList<>();
                for (int i = 0; i < productData.size(); i++) {
                    productIds.add(productData.get(i).getmId().toString());
                }
                wo.setmProductId(productIds);
            }
            if (timeTv.getText().toString().equals("请选择时间")) {
                wo.setmPlanAt("");
            } else {
                wo.setmPlanAt(timeTv.getText().toString());
            }
            wo.setmContactName(customerUser.getName());
            wo.setmContactPhone(customerUser.getPhone());
            wo.setmDescription(faultInfo.getText().toString());
            wo.setmLevel("普通");
            wo.setmTypeName(typeNameArray[0]);
            wo.setmServiceContent(serviceContent);
            wo.setmServiceType(serviceType);
            wo.setmEventId(eventId);
            if (masterUser.getId() != null || helperList.size() != 0) {
                Toast.makeText(this, "仅保存不保存负责人和协助人", Toast.LENGTH_SHORT).show();
            }
            uploadFile(faultImgUrls, wo);
        });
    }


    private void initView() {
        timeTv = findViewById(R.id.time);
        customerName = findViewById(R.id.customer_name);
        customerContactsName = findViewById(R.id.customer_contacts_name);
        customerPhoneNum = findViewById(R.id.customer_phone_info);
        customerAddress = findViewById(R.id.customer_address_info);
        customerAddLay = findViewById(R.id.customer_add);
        customerInfoLay = findViewById(R.id.customer_info_item);
        imBack = findViewById(R.id.work_order_img_back);
        productAddImg = findViewById(R.id.product_add_img);
        productList = findViewById(R.id.product_list);
        serviceContentSpinner = findViewById(R.id.service_content_spinner);
        serviceTypeSpinner = findViewById(R.id.service_type_option_item);
        faultPhoto = findViewById(R.id.fault_photo_layout);
        directoryImg = findViewById(R.id.work_order_person_img);
        staffName = findViewById(R.id.work_order_person_name);
        workOrderPersonLay = findViewById(R.id.work_order_person_item);
        helperName = findViewById(R.id.work_order_helper_name);
        helperLay = findViewById(R.id.work_order_helper_item);
        createCustomer = findViewById(R.id.customer_create_tx);
        customerContactsNameLay = findViewById(R.id.customer_contacts_tx);
        linearLayout = findViewById(R.id.linearLayout);
        createCustomerUser = findViewById(R.id.customer_contacts_create_tx);
        saveBtn = findViewById(R.id.save);
        faultInfo = findViewById(R.id.fault_info);
        faultImgList = findViewById(R.id.fault_img_list);
        faultInfoItem = findViewById(R.id.fault_item);
        createProduct = findViewById(R.id.product_create_tx);
        saveOnlyBtn = findViewById(R.id.save_only);
    }

    private void initData() {
        customerUser = new CustomerUserResponse();
        masterUser = new DepartmentsUserResponse();
        customerItem = new CustomerResponse();
        faultImgUrls = new ArrayList<>();
        helperList = new ArrayList<>();
        productData = new ArrayList<>();
        customerUserResponses = new ArrayList<>();
        intent = getIntent();

        imgListViewAdapter = new ImgListViewAdapter(MaintainActivity.this, faultImgUrls);
        faultImgList.setAdapter(imgListViewAdapter);
        helperAdapter = new HelperListAdapter(this, R.layout.product_item_list, helperList);
        helperName.setAdapter(helperAdapter);
        productListAdapter = new ProductListAdapter(MaintainActivity.this, R.layout.product_item_list, productData);
        productList.setAdapter(productListAdapter);
        customerUserAdapter = new CustomerUserAdapter(this, R.layout.customer_user_item_list, customerUserResponses);

        if (intent.getSerializableExtra("customer") != null) {
            customerItem = (CustomerResponse) intent.getSerializableExtra("customer");
            customerName.setText(customerItem.getName());
            customerAddress.setText(customerItem.getDetail_address());
        }
        if (intent.getSerializableExtra("customerUser") != null) {
            customerUser = (CustomerUserResponse) intent.getSerializableExtra("customerUser");
            customerContactsName.setText(customerUser.getName());
            customerPhoneNum.setText(customerUser.getPhone());
            customerAddLay.setVisibility(View.GONE);
            customerInfoLay.setVisibility(View.VISIBLE);
        }
        if (intent.getSerializableExtra("product") != null) {
            productData.add((ProductResponse) intent.getSerializableExtra("product"));
            ViewUtils.setListViewHeightBasedOnChildren(productList);
            productList.setVisibility(View.VISIBLE);
            productListAdapter.notifyDataSetChanged();
        }
        eventId = intent.getStringExtra("eventId");
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (PermissionUtils.isPermissionGranted(this)) {
            Log.i("PERMISSION", "请求权限成功");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PermissionUtils.REQUEST_CODE) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    Log.e("Permission", "授权失败！");
                    // 授权失败，退出应用
                    this.finish();
                    return;
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == -1) {
            customerAddLay.setVisibility(View.GONE);
            customerInfoLay.setVisibility(View.VISIBLE);
            customerItem = (CustomerResponse) data.getSerializableExtra("customerItem");
            //选择不同的客户清空原有的产品列表
            if (customerItem.getName() != customerName.getText()) {
                productData.clear();
                customerUserResponses.clear();
                productListAdapter.notifyDataSetChanged();
                productList.setVisibility(View.GONE);
            }
            customerName.setText(customerItem.getName());
            if (customerItem.getUser() != null) {
                customerUser.setName(customerItem.getUser().getName());
                customerUser.setPhone(customerItem.getUser().getPhone());
                customerContactsName.setText(customerItem.getUser().getName());
                customerPhoneNum.setText(customerItem.getUser().getPhone());
            } else {
                customerContactsName.setText("");
                customerPhoneNum.setText("");
                ImmUtils.setEditTextHintFontSize(customerContactsName, 15, "暂无");
                ImmUtils.setEditTextHintFontSize(customerPhoneNum, 15, "暂无");
            }
            customerAddress.setText(customerItem.getDetail_address());

        }
        if (requestCode == 2 && resultCode == -1) {
            productData.clear();
            productData.addAll((ArrayList<ProductResponse>) data.getSerializableExtra("result"));
            productListAdapter.notifyDataSetChanged();
            ViewUtils.setListViewHeightBasedOnChildren(productList);
            productList.setVisibility(View.VISIBLE);

        }
        if (requestCode == PictureConfig.CHOOSE_REQUEST && resultCode == RESULT_OK && null != data) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            for (LocalMedia localMedia : selectList) {
                faultImgUrls.add(localMedia.getPath());
            }
            imgListViewAdapter.notifyDataSetChanged();
            faultImgList.setVisibility(View.VISIBLE);
        }
        if (requestCode == PictureConfig.REQUEST_CAMERA && resultCode == RESULT_OK && null != data) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            for (LocalMedia localMedia : selectList) {
                faultImgUrls.add(localMedia.getPath());
                System.out.println(localMedia.getPath());
            }
            imgListViewAdapter.notifyDataSetChanged();
            faultImgList.setVisibility(View.VISIBLE);
        }
    }

    //创建dialog
    private Dialog createDialog(View dialogView) {
        Dialog dialog = new Dialog(this, R.style.background_darkens_dialog_style);
        dialog.setContentView(dialogView);
        dialog.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(this).getScreenWidth());
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
        return dialog;
    }

    //请求用户联系人
    private void getCustomerUser(int id) {
        customerUserRequest.getCustomerUsers(id, (res) -> {
            customerUserResponses.clear();
            customerUserResponses.addAll(res.getData());
            customerUserAdapter.notifyDataSetChanged();
            loadingDialog.dismiss();
        }, (failedMessage) -> {

        });
    }

    //上传照片
    private void uploadFile(List<String> urls, WorkOrderRequest workOrderRequest) {
        List<String> uploadImgUrls = new ArrayList<>();
        final Dialog dialog = new Dialog(this, R.style.normal_dialog_style);
        View dialogView = View.inflate(this, R.layout.dialog_loading, null);
        dialog.setContentView(dialogView);
        //使得点击对话框外部不消失对话框
        dialog.setCanceledOnTouchOutside(false);
        AVLoadingIndicatorView avi = dialogView.findViewById(R.id.avi);
        avi.show();
        dialog.show();

        if (urls.size() == 0) {
            createWorkOrder(workOrderRequest, dialog, uploadImgUrls);
        } else {
            for (int i = 0; i < urls.size(); i++) {
                File file = new File(urls.get(i));
                FileRequest request = new FileRequest();
                int index = i;
                request.uploadFile(file, res -> {
                    uploadImgUrls.add(res.getData().getPath());
                    if (index == urls.size() - 1) {
                        createWorkOrder(workOrderRequest, dialog, uploadImgUrls);
                    }
                }, failedMessage -> {
                    dialog.dismiss();
                    Toast.makeText(this, failedMessage.toString(), Toast.LENGTH_SHORT).show();
                });
            }
        }
    }

    //创建工单
    private void createWorkOrder(WorkOrderRequest workOrderRequest, Dialog dialog, List<String> imgUrls) {
        workOrderRequest.setmImages(imgUrls);
        Retrofit mRetrofit = RetrofitClient.getInstance();
        WorkOrderApi workOrderApi = mRetrofit.create(WorkOrderApi.class);
        Call<BaseResponse<Void>> createOrder = workOrderApi.createOrder(workOrderRequest);
        AsyncRequest<BaseResponse<Void>> requestOrder = new AsyncRequest<>();
        requestOrder.sendRequest(createOrder, resOrder -> {
            dialog.dismiss();
            Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show();
            if (isEventCreateWork) {
                Intent intent = new Intent(this, ServiceEventInfoActivity.class);
                intent.putExtra("eventId", eventId);
                startActivity(intent);
            }
            finish();
        }, err -> {
            dialog.dismiss();
            Toast.makeText(this, err.toString(), Toast.LENGTH_SHORT).show();
        });
    }

    public void loadingShow() {
        loadingDialog = new Dialog(this, R.style.normal_dialog_style);
        View loadingView = View.inflate(this, R.layout.dialog_loading, null);
        loadingDialog.setContentView(loadingView);
        //使得点击对话框外部不消失对话框
        loadingDialog.setCanceledOnTouchOutside(false);
        AVLoadingIndicatorView avi = loadingView.findViewById(R.id.avi);
        avi.show();
        loadingDialog.show();
    }
}